<template>
  <scroll-view
    v-if="Object.values(data).length"
    scroll-y="true"
    class="content"
  >
    <uni-section title="采购信息" type="line">
      <uni-card :title="data.spareName" class="card">
        <text class="card-item" v-for="(i, z) in columns" :key="z"
          >{{ i.name }}：{{ data[i.field] }}</text
        >
      </uni-card>
    </uni-section>

    <uni-section v-if="attachmentList?.length" title="附件信息" type="line">
      <uni-list>
        <uni-list-item
          ellipsis="1"
          v-for="(i, z) in attachmentList"
          :key="z"
          :title="i.attachmentName"
        >
          <template v-slot:footer>
            <text
              class="download"
              @click="downloadFile(`${baseUrl}${i?.attachmentPath}`)"
              >查看</text
            >
          </template>
        </uni-list-item>
      </uni-list>
    </uni-section>

    <uni-section title="报价明细信息" type="line">
      <uni-card class="card" v-for="(item, j) in tableData" :key="j">
        <view class="spec-item">
          <view class="number">
            序号：{{ j + 1 }}
            <text v-if="item.isIntend === '1'">意向供应商</text>
          </view>
          <text class="card-item" v-for="(i, z) in confirmColumns" :key="z">
            <text class="title">{{ i.title }}</text
            >：{{ item[i.dataIndex] }}</text
          >
         <view v-if="this.approvalStatus == '0'" class="action-detail" style="flex-basis: 100%">
           <button
             :class="
               item.isConfirm == '1'
                 ? 'bg-grey text-color-grey'
                 : 'color-audit'
             "
             @click="confirm(item.id, item.isConfirm)"
           >
           {{item.isConfirm == '1' ? '取消确认':'确认'}}  
           </button>
         </view>
         
         <view v-else class="action-detail" style="flex-basis: 100%">
         			  <button :class="
               item.isConfirm == '1'
                 ? 'color-audit'
                 : 'bg-grey text-color-grey'
             ">
         				  {{item.isConfirm == '1' ? '已选择':'未选择'}}  
         			  </button>
         </view>
        </view>
      </uni-card>
    </uni-section>
  </scroll-view>

  <view v-else class="no-data"> 暂无数据 </view>
</template>
  
  <script>
import { TOKEN } from "@/utils/auth";
import { downloadFile } from "@/utils/download";
import { baseUrl } from "@/utils/httpInterceptor";
import { Toast } from "@/utils/uniHooks/toast";
import request from "@/utils/httpInterceptor";

export default {
  data() {
    return {
      data: {}, //路由参数
	  approvalStatus:'',
      confirmColumns: [
        {
          title: "供应商名称",
          dataIndex: "supplierName",
        },
        { title: "联系人", dataIndex: "supplierContactsName" },
        { title: "报价单价(元)", dataIndex: "singlePrice" },
        { title: "报价金额(元)", dataIndex: "amount" },
        {
          title: "是否含税",
          dataIndex: "hasTax",
        },
        { title: "税率(%)", dataIndex: "taxRate" },
        { title: "保质期(天)", dataIndex: "expireDays" },
        { title: "报价人", dataIndex: "creatorName" },
        {
          title: "报价时间",
          dataIndex: "createTime",
        },
        { title: "意向人", dataIndex: "intendPersonNames" },
        { title: "确认人", dataIndex: "confirmPersonName" },
        { title: "供应商地址", dataIndex: "address" },
        { title: "备注", dataIndex: "remarks" },
        // {
        //   title: "来源",
        //   dataIndex: "source",
        // },
      ],
      tableData: [],
      attachmentList: [],
      baseUrl,
    };
  },

  computed: {},
  onLoad: function (option) {
    uni.showLoading({
      title: "加载中",
    });
    uni.setNavigationBarTitle({
      title: option?.billTypeName || "详情",
    });
	this.approvalStatus = option?.approvalStatus
    this.data = JSON.parse(option.itemData);
    this.columns = this.data._columns_;
    this.getConfirmData();
    this.getAttachmentList();
  },

  methods: {
    downloadFile,

    async confirm(id, isConfirm) {
		let url = `/spare/spare-purchase-quotation/confirm`;
		if(isConfirm == '1'){
			url = `/spare/spare-purchase-quotation/cancel/confirm`;
		}
      const res = await request({
        url: url,
        method: "PUT",
        data: {
          auditBillItemId: this.data.id,
          id,
        },
      });
      if (res?.code == "0") {
        Toast(res?.msg || "操作成功");
        this.getConfirmData();
      }
    },

    async getAttachmentList() {
      const res = await request({
        url: `/attachment/biz/query`,
        method: "POST",
        data: {
          businessId: this.data.businessId,
          spareId: this.data.businessId,
          businessType: "BPBJ",
        },
      });
      if (res?.data?.length) {
        this.attachmentList = res.data;
      }
    },
    async getConfirmData() {
      const res = await request({
        url: `/spare/spare-purchase-quotation/listByApplyItemId/${this.data.applyBillItemId}`,
        method: "GET",
      });
      if (res?.data?.length) {
        this.tableData = res.data;
      }
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.action-number {
  padding: 0 10px;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  position: fixed;
  bottom: 45px;
  width: 100%;
  border-top: 1px solid #eee;
  height: 35px;
  align-items: center;
}
</style>
  